package com.shu.qq.data

import android.util.Log
import com.shu.qq.data.bean.BaseResponse
import com.shu.qq.data.bean.LoginResponse
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.RequestBody
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.Headers
import retrofit2.http.POST

const val TAG = "shu-check-QQService"
interface QQService {

    @Headers("$AUTHORIZATION_HEADER: login")
    @POST("/login")
    suspend fun login(@Body request:RequestBody): LoginResponse

    @Headers("$AUTHORIZATION_HEADER: register")
    @POST("/register")
    suspend fun register(@Body request:RequestBody): BaseResponse

    companion object {
        const val AUTHORIZATION_HEADER = "authorization"
        private const val BASE_URL = "http://192.168.0.222:8080"
        fun create(installIn: Interceptor): QQService {
            Log.d(TAG, "create: 创建Retrofit网络请求 ${installIn.hashCode()}")
            val logger =
                HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }
            val client = OkHttpClient.Builder()
                .addInterceptor(logger)
                .addInterceptor(installIn)
                .build()
            val service = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(QQService::class.java)
            Log.d(TAG, "create: 创建的service为：${service.hashCode()}")
            return service
        }
    }
}